package com.example.bilibili_fake.ui.home

import BannerRepository
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.bannerapi.model.BannerItem
import com.example.bilibili_fake.data.model.Archive

import com.example.bilibili_fake.data.repository.VideoRepository
import kotlinx.coroutines.launch

class HomeViewModel : ViewModel() {

    private val videoRepository = VideoRepository()

    /*private val _text = MutableLiveData<String>().apply {
        value = "This is home Fragment"
    }*/
    /*val text: LiveData<String> = _text*/
    private val bannerRepository = BannerRepository()

    private val _videos = MutableLiveData<List<Archive>>()
    val videos: LiveData<List<Archive>> = _videos

    private val _loading = MutableLiveData<Boolean>()
    val loading: LiveData<Boolean> = _loading

    private val _error = MutableLiveData<String?>()
    val error: LiveData<String?> = _error
    private val _banners = MutableLiveData<List<BannerItem>>()
    val banners: LiveData<List<BannerItem>> = _banners
    private val _refreshing = MutableLiveData<Boolean>()
    val refreshing: LiveData<Boolean> get() = _refreshing
    private var currentPage = 1
    fun loadVideos() {
        viewModelScope.launch {
            _loading.value = true
            _error.value = null
            videoRepository.fetchPopularVideos(pageSize = 8, pageNumber = currentPage).fold(
                onSuccess = { videoList ->
                    _videos.value = videoList
                    _loading.value = false
                },
                onFailure = { exception ->
                    _error.value = exception.message
                    _loading.value = false
                }
            )
        }
    }

    fun loadBanners() {
        viewModelScope.launch {
            bannerRepository.getBannerList().fold(
                onSuccess = { bannerList ->
                    _banners.value = bannerList
                },
                onFailure = { exception ->
                    _error.value = exception.message
                }
            )
        }
    }

    fun loadMoreVideos() {
        viewModelScope.launch {
            _loading.value = true
            _error.value = null
            // 传递下一页的页码参数
            videoRepository.fetchPopularVideos(pageSize = 8, pageNumber = currentPage + 1).fold(
                onSuccess = { videoList ->
                    val currentVideos = _videos.value ?: emptyList()
                    _videos.value = currentVideos + videoList
                    currentPage++ // 成功后再更新页码
                    _loading.value = false
                },
                onFailure = { exception ->
                    _error.value = exception.message
                    _loading.value = false
                }
            )
        }
    }

    fun refreshVideos() {
        _refreshing.value = true
        viewModelScope.launch {
            try {
                loadBanners()
                loadVideos()

            } finally {
                _refreshing.value = false // 确保最终关闭刷新状态
            }
        }
    }
}

